import { _decorator, Component, instantiate, Node, NodePool, Prefab } from 'cc';
const { ccclass, property } = _decorator;

interface IDictPool{
    [name: string]: NodePool;
}

interface IDictPrefab{
    [name: string]: Prefab;
}

export class PoolMananger {
    private static instance: PoolMananger = null;
    private _dictPool: IDictPool = {};
    private _dictPrefab: IDictPrefab = {};

    public static getInstance(): PoolMananger {
        if(PoolMananger.instance === null){
            PoolMananger.instance = new PoolMananger();
        }
        return PoolMananger.instance;
    }

    public getNode(prefab: Prefab, parent: Node){
        let name = prefab.data.name;
        let node: Node = null;
        this._dictPrefab[name] = prefab;
        const pool = this._dictPool[name];
        if(pool){
            if(pool.size() > 0){
                node = pool.get();
            } else {
                node = instantiate(prefab);
            }
        }else{
            this._dictPool[name] = new NodePool();
            node = instantiate(prefab);
        }
        node.parent = parent;
        node.active = true;
        return node;
    }

    public putNode(node: Node){
        let name = node.name;
        node.parent = null;
        const pool = this._dictPool[name];
        if(!pool){
            this._dictPool[name] = new NodePool();
        }
        pool.put(node);
    }
}


